package com.ranyk.www.api.admin;

import com.ranyk.www.base.BaseController;
import com.ranyk.www.entity.AdminUser;
import com.ranyk.www.response.ResponseResult;
import com.ranyk.www.security.AuthorizingUser;
import com.ranyk.www.service.IAdminUserLoginLogService;
import com.ranyk.www.service.IAdminUserService;
import com.ranyk.www.support.page.PageInfo;
import com.ranyk.www.util.SingletonLoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * CLASS_NAME: AdminController.java <br/>
 *
 * @author ranyk           <br/>
 * @version V1.0           <br/>
 * @decription: 管理员个人信息表示层控制器  <br/>
 * @date: 2022-04-13 <br/>
 */
@Slf4j
@RestController
@RequestMapping("/admin/info/")
@Api(value = "管理员个人信息", tags = "管理员个人信息请求处理")
public class AdminController extends BaseController {

    /**
     * 管理员业务对象
     */
    private IAdminUserService adminUserService;
    /**
     * 管理员登录日志业务对象
     */
    private IAdminUserLoginLogService adminUserLoginLogService;

    /**
     * 管理员业务对象 set Bean 注入
     *
     * @param adminUserService 管理员业务对象
     */
    @Autowired
    public void setAdminUserService(IAdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    /**
     * 管理员登录业务对象 set Bean 注入
     *
     * @param adminUserLoginLogService 管理员登录业务对象
     */
    @Autowired
    public void setAdminUserLoginLogService(IAdminUserLoginLogService adminUserLoginLogService) {
        this.adminUserLoginLogService = adminUserLoginLogService;
    }

    /**
     * 查看用户信息接口
     *
     * @return 返回请求响应结果封装对象 {@link  ResponseResult}
     */
    @GetMapping(value = "/view")
    @RequiresPermissions("administrator:info:view")
    @ApiOperation(value = "管理员个人信息", notes = "管理员信息/管理员日志列表/管理员权限")
    public ResponseResult view() {
        return ResponseResult.valueOfObject(adminUserService.getAdminInfo(getCurrentUserId()));
    }

    /**
     * 查询单个管理员的登录信息
     *
     * @param pageInfo 分页 {@link PageInfo} 对象
     * @param search   查询条件
     * @return 返回请求响应结果封装对象 {@link ResponseResult}
     */
    @GetMapping(value = "/logs")
    @RequiresPermissions("administrator:info:view")
    @ApiOperation(value = "管理员个人登录日志", notes = "管理员个人登录日志")
    @ApiResponses({@ApiResponse(responseCode = "200", description = "响应成功")})
    public ResponseResult logs(@ApiParam(value = "请求分页对象") PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
        Map<String, Object> result = adminUserLoginLogService.getLogsByUserId(getCurrentUserId(), pageInfo, search);
        return ResponseResult.valueOfObject(result);
    }

    /**
     * 更新管理员个人信息
     *
     * @param user 需更新的用户信息封装对象
     * @return 返回请求响应结果封装对象 {@link ResponseResult}
     */
    @PutMapping(value = "/update")
    @RequiresPermissions("administrator:info:edit")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "响应成功")})
    @ApiOperation(value = "更新管理员个人信息", notes = "管理员名/真实姓名/性别/年龄/电话/电子邮箱")
    public ResponseResult update(AdminUser user) {
        AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
        if (authorizingUser != null) {
            user.setUserId(authorizingUser.getUserId());
            user.setUpdateBy(authorizingUser.getUserName());
            int count = adminUserService.updateByUserId(user);
            return ResponseResult.builder().object(count).build();
        } else {
            return ResponseResult.builder().msg("未获取到登录用户信息").build();
        }
    }

}
